package com.ae.content.service.impl;

import com.ae.content.service.AliexpressService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class AliexpressServiceImpl implements AliexpressService {

    @Override
    public void scheduleJob() {
        try {
            // 执行 whoami 命令
            Process process = Runtime.getRuntime().exec("who");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));

            // 读取并输出命令执行结果
            final String[] result = new String[1];
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("当前登录的用户是: " + line);
                Pattern pattern = Pattern.compile("\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b");
                Matcher matcher = pattern.matcher(line);

                System.out.println("Found IPs:");
                while (matcher.find()) {
                    System.out.println(matcher.group());
                    result[0] = matcher.group();
                    // 获取本机的IP地址
                    try {
                        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                        while (interfaces.hasMoreElements()) {
                            NetworkInterface networkInterface = interfaces.nextElement();
                            // 过滤掉 loopback 地址（即 127.0.0.1）和非激活的接口
                            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                                continue;
                            }
                            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                            while (addresses.hasMoreElements()) {
                                InetAddress inetAddress = addresses.nextElement();
                                // 过滤掉IPv6地址，只显示IPv4地址
                                if (inetAddress.getAddress().length == 4) {
                                    System.out.println(networkInterface.getName() + ": " + inetAddress.getHostAddress());
                                }
                            }
                        }
                        // todo 请求接口写入最新的服务器的用户登录情况
                    } catch (SocketException e) {
                        System.out.println("Error getting network interfaces: " + e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
            // 等待进程结束
            process.waitFor();
        } catch (IOException e) {
            System.err.println("发生IO异常: " + e.getMessage());
        } catch (InterruptedException e) {
            System.err.println("进程被中断: " + e.getMessage());
        }
    }
}
